//
//  LocationViewController.m
//  TravelDiary
//
//  Created by YDB MAC on 15/11/12.
//  Copyright © 2015年 YDB MAC. All rights reserved.
//

#import "LocationViewController.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
@interface LocationViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{

    BMKMapView *_mapView;
    BMKLocationService *_locService;//
    BMKGeoCodeSearch *_geoCode;
    
}
@property(nonatomic, strong)UIButton *followingButton;//跟随状态
@property(nonatomic, strong)UIButton *followHeadingButton;//罗盘状态
@property(nonatomic, strong)UIButton *stopButton;//停止定位
@property(nonatomic, strong)NSString *locationStr;//存储反编码的地理位置


@end

@implementation LocationViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = _titleStr;
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.view = _mapView;
    
//添加跟随状态下的按钮
    _followingButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _followingButton.frame = CGRectMake(20, self.view.frame.size.height-70,self.view.frame.size.width/2-40,30);
    [_followingButton setTitle:@"跟随状态" forState:(UIControlStateNormal)];
    [_followingButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    _followingButton.layer.masksToBounds = YES;
    _followingButton.layer.cornerRadius = 10;
    _followingButton.layer.borderWidth = 1;
    _followingButton.layer.borderColor = [UIColor blueColor].CGColor;
    _followingButton.backgroundColor = [UIColor whiteColor];
  
    [_mapView addSubview:_followingButton];
    [_followingButton addTarget:self action:@selector(followingButtonclick:) forControlEvents:(UIControlEventTouchUpInside)];
    
//添加罗盘状态下的按钮
    
    _followHeadingButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _followHeadingButton.frame = CGRectMake(CGRectGetMaxX(_followingButton.frame)+20, self.view.frame.size.height-70,self.view.frame.size.width/2-40,30);
    [_followHeadingButton setTitle:@"罗盘状态" forState:(UIControlStateNormal)];
    [_followHeadingButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
     _followHeadingButton.layer.masksToBounds = YES;
     _followHeadingButton.layer.cornerRadius = 10;
     _followHeadingButton.layer.borderWidth = 1;
     _followHeadingButton.layer.borderColor = [UIColor blueColor].CGColor;
     _followHeadingButton.backgroundColor = [UIColor whiteColor];

    [_mapView addSubview:_followHeadingButton];
    [_followHeadingButton addTarget:self action:@selector(followHeadingButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
//设置定位停止
//    _stopButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    _stopButton.frame = CGRectMake(CGRectGetMaxX(_followHeadingButton.frame)+20, self.view.frame.size.height-70,self.view.frame.size.width/3-30,50);
//    [_stopButton setTitle:@"停止" forState:(UIControlStateNormal)];
//    [_mapview addSubview:_stopButton];
//    [_stopButton addTarget:self action:@selector(stopButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];

    
//     [_mapview setMapType:BMKMapTypeSatellite];//设置地图为卫星图
//     [_mapview setMapType:BMKMapTypeStandard];//设置地图为矢量图
//       [_mapview setTrafficEnabled:YES];//打开实时路况图层，默认为关闭
//      [_mapview setBaiduHeatMapEnabled:YES];
    [self createMapView];
   }

-(void)createMapView{
    
    _geoCode = [[BMKGeoCodeSearch alloc] init];
    _geoCode.delegate = self;

    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    [_locService startUserLocationService];
    _mapView.zoomLevel=17;
    _mapView.showsUserLocation = NO;//先关闭显示的定位图层
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
    _mapView.showsUserLocation = YES;//显示定位图层
    
    [_stopButton setEnabled:YES];
    [_stopButton setAlpha:1.0];
    [_followHeadingButton setEnabled:YES];
    [_followHeadingButton setAlpha:1.0];
    [_followingButton setEnabled:YES];
    [_followingButton setAlpha:1.0];
    
  
}
//进入跟随状态
-(void)followingButtonclick:(UIButton *)sender{

    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;
    _mapView.showsUserLocation = YES;

}
//进入罗盘状态
-(void)followHeadingButtonClick:(UIButton *)sender{

    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
    _mapView.showsUserLocation = YES;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [_mapView viewWillAppear];
    _mapView.delegate = self;//不用时候置为nil
    _locService.delegate = self;
    _geoCode.delegate = self;


}
-(void)viewWillDisappear:(BOOL)animated{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;//不用时候置为nil
    _locService.delegate = nil;
    _geoCode.delegate = nil;
}
-(void)zoomMap:(id)sender
{
    _mapView.zoomLevel = _mapView.zoomLevel++;
}
/**
 *在地图View将要启动定位时，会调用此函数
 *@param mapView 地图View
 */
- (void)willStartLocatingUser
{
   
}

/**
 *用户方向更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
   }

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
//   
//    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);\
    
    
    // 初始化反地址编码选项（数据模型）
    BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init];
    // 将获得的经纬度的数据传到反地址编码模型
    option.reverseGeoPoint = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    // 调用反地址编码方法，让其在代理方法中输出
    [_geoCode reverseGeoCode:option];
    userLocation.title = _locationStr;
    [_mapView updateLocationData:userLocation];
    
    
}
#pragma mark 代理方法返回反地理编码结果
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (result) {
        _locationStr = [NSString stringWithFormat:@"%@ - %@", result.address, result.addressDetail.streetNumber];
        }else{
            
          }
}

/**
 *在地图View停止定位后，会调用此函数
 *@param mapView 地图View
 */
- (void)didStopLocatingUser
{
   
}

/**
 *定位失败后，会调用此函数
 *@param mapView 地图View
 *@param error 错误号，参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
   
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
